
<template>
  <el-card style="margin-bottom: 5px">
    <el-input clearable @clear="load" style="width: 260px; margin-right: 5px;" v-model="data.fenlei" placeholder="请输入分类名查询" :prefix-icon="Search"></el-input>
    <el-button type="primary" @click="load">查 询</el-button>
    <el-button type="info" @click="reset">重 置</el-button>
  </el-card>
  <el-card style="margin-bottom: 5px">
    <el-button type="primary" @click="headleAdd">新   增</el-button>
  </el-card>
  <el-card>
    <el-table :data="data.tableData" style="width: 100%" :header-cell-style="{ color:'#333',backgroundColor:'#dde8ff'}">
      <el-table-column width="100" prop="adminName" label="上传人" />
      <el-table-column width="100" prop="fenlei" label="商品分类" />
      <el-table-column prop="time" label="创建时间" />
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button type="primary" icon="Edit" circle @click="headleEdit(scope.row)" ></el-button>
          <el-button type="danger" icon="Delete" circle @click="del(scope.row.id)" ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 使用 Dialog 组件 -->
  <Dialog
      v-model="data.formVisible"
      :form="data.form"
      @save="load"
  />
</template>

<script setup>import { Search } from "@element-plus/icons-vue";
import { reactive } from "vue";
import request from "@/utils/request";
import Dialog from "./Dialog.vue"

const data = reactive({
  fenlei: "",
  formVisible: false,
  pageNum: 1,
  pageSize: 5,
  total: 6,
  tableData: [],
  form: {}
});

const load = () => {
  request.get('shangpinfenlei/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      fenlei: data.fenlei
    }
  }).then(res => {
    data.tableData = res.data.list;
  }).catch(err => {
    ElMessage.error(err);
  });
}

load()

// 重置按钮
const reset = () => {
  data.fenlei = ''
  load()
}

// 新增按钮
const headleAdd = () => {
  data.form = {
    fenlei: ''
  }
  data.formVisible = true
}

// 修改按钮
const headleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

// 删除按钮
const del = (id) => {
  ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '删除确认', { type: 'warning' }).then(res => {
    request.delete('/shangpinfenlei/delete/' + id).then(res => {
      if (res.code === '200') {
        ElMessage.success('删除成功')
        load()
      }
    }).catch(() => {
      ElMessage.error(res.msg)
    })
  }).catch(() => {
    ElMessage.info('已取消删除')
  });
}
</script>